package com.example.casper.Experiment2024.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private Thread thread;
    private boolean running;
    private List<Bomb> bombs;
    private Submarine submarine;
    private BombCountListener bombCountListener;
    private GameOverListener gameOverListener;

    public interface BombCountListener {
        void onBombCountUpdate(int count);
    }

    public interface GameOverListener {
        void onGameOver();
    }

    public GameSurfaceView(Context context) {
        super(context);
        init();
    }

    public GameSurfaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GameSurfaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        getHolder().addCallback(this);
        bombs = new ArrayList<>();
        submarine = new Submarine();
    }

    public void setBombCountListener(BombCountListener listener) {
        this.bombCountListener = listener;
    }

    public void setGameOverListener(GameOverListener listener) {
        this.gameOverListener = listener;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Handle surface changes if needed
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        running = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        while (running) {
            Canvas canvas = null;
            try {
                canvas = getHolder().lockCanvas();
                synchronized (getHolder()) {
                    update();
                    draw(canvas);

                    if (bombCountListener != null) {
                        bombCountListener.onBombCountUpdate(bombs.size());
                    }
                }
            } finally {
                if (canvas != null) {
                    getHolder().unlockCanvasAndPost(canvas);
                }
            }

            // Add some delay to simulate game loop
            try {
                Thread.sleep(30); // Adjust this value to control the game speed
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // Game over logic
        if (gameOverListener != null) {
            gameOverListener.onGameOver();
        }
    }

    private void update() {
        // Update game logic here
        // For example, spawn new bombs, check for collisions, etc.

        // Placeholder for collision detection
        if (Math.random() < 0.01) { // Random chance for a game over
            running = false; // Simulate a game over for demo purposes
        }
    }

    public void draw(Canvas canvas) {
        // Draw game objects here
        // Placeholder drawing
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawRect(submarine.getX(), submarine.getY(), submarine.getX() + 50, submarine.getY() + 50, paint);

        for (Bomb bomb : bombs) {
            paint.setColor(Color.RED);
            canvas.drawRect(bomb.getX(), bomb.getY(), bomb.getX() + 50, bomb.getY() + 50, paint);
        }
    }

    // Placeholder classes for Bomb and Submarine
    private static class Bomb {
        private int x, y;

        public Bomb(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }
    }

    private static class Submarine {
        private int x = 100, y = 400; // Example position

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        // Methods to move the submarine would be here
    }

    // Methods to resume and pause the game could be added here if needed
    public void resume() {
        // Resume game logic if paused
    }

    public void pause() {
        // Pause game logic if needed
        running = false;
    }
}